package cn.tedu.knows.sys.mapper;


import cn.tedu.knows.commons.model.Permission;
import cn.tedu.knows.commons.model.Role;
import cn.tedu.knows.commons.model.User;
import cn.tedu.knows.sys.vo.UserVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-23
 */
@Repository
public interface UserMapper extends BaseMapper<User> {


    //根据用户名查询用户信息
    @Select("select * from user where username=#{username}")
    User findUserByUsername(String username);

    //根据用户id查询这个用户的所有权限
    @Select("SELECT p.id, p.name" +
            " FROM user u" +
            " LEFT JOIN user_role ur ON u.id=ur.user_id" +
            " LEFT JOIN role r ON r.id=ur.role_id" +
            " LEFT JOIN role_permission rp " +
            " ON r.id=rp.role_id" +
            " LEFT JOIN permission p" +
            " ON p.id=rp.permission_id" +
            " WHERE u.id=#{id}")
    List<Permission> findUserPermissionsById(Integer id);


    // 根据用户名查询UserVo类型对象
    // UserVo用户信息只有id,username,nickname
    // 这个方法的返回值一定要是UserVo类型
    @Select("select id,username,nickname from " +
                                "user where username=#{username}")
    UserVo findUserVoByUsername(String username);

    //根据用户id查询用户角色的方法

    @Select("SELECT r.id , r.name" +
            " FROM user u" +
            " LEFT JOIN user_role ur ON ur.user_id=u.id" +
            " LEFT JOIN role r ON r.id=ur.role_id" +
            " WHERE u.id=#{id}")
    List<Role> findUserRolesById(Integer id);





}
